import 'dart:convert';
import 'dart:io';

/// Helper class for loading test data from JSON files
class TestJsonDataLoader {
  /// Load test data from a JSON file
  ///
  /// [filePath] should be relative to the test directory
  /// Returns a List<Map<String, dynamic>> that can be used for testing
  static List<Map<String, dynamic>> loadJsonData(String filePath) {
    final jsonFile = File('test/json/$filePath');
    if (!jsonFile.existsSync()) {
      throw FileSystemException('Test data file not found', jsonFile.path);
    }

    final jsonString = jsonFile.readAsStringSync();
    return List<Map<String, dynamic>>.from(jsonDecode(jsonString));
  }

  /// Load a single object from JSON file
  ///
  /// [filePath] should be relative to the test directory
  /// Returns a Map<String, dynamic> for a single object
  static Map<String, dynamic> loadSingleJsonData(String filePath) {
    final jsonFile = File('test/json/$filePath');
    if (!jsonFile.existsSync()) {
      throw FileSystemException('Test data file not found', jsonFile.path);
    }

    final jsonString = jsonFile.readAsStringSync();
    return Map<String, dynamic>.from(jsonDecode(jsonString));
  }

  /// Load test data from a JSON file as a specific type
  ///
  /// [filePath] should be relative to the test directory
  /// [fromJson] function to convert JSON to the desired type
  static List<T> loadTypedJsonData<T>(
    String filePath,
    T Function(Map<String, dynamic>) fromJson,
  ) {
    final jsonData = loadJsonData(filePath);
    return jsonData.map(fromJson).toList();
  }
}
